package com.lyt.springboot.util.algorithm;

import java.math.BigInteger;

public class S17 {
    //求String 大数和
    public static void main(String[] args) {
        S17 s17 = new S17();
        s17.solve("9","99999999999999999999999999999999999999999999999999999999999994");
    }
    public String solve(String s,String t) {
        //长字符串  短字符串
        String longStr;
        String shortStr;
        boolean ifDiff = true;
        if(s.length()>t.length()) {
            longStr = s;
            shortStr = t;
        }else if(s.length()<t.length()) {
            longStr = t;
            shortStr = s;
        }else {
            longStr = t;
            shortStr = s;
            ifDiff  = false;
        }
        //定义carry
        StringBuilder builder = new StringBuilder();
        int carry = 0;
        int i;
        int j;
        for (i = shortStr.length()-1,j =longStr.length()-1;i>=0;i--,j--) {
           int c1 =  longStr.charAt(j)-'0';
           int c2 = shortStr.charAt(i)-'0';
           int cur = c1+c2+carry;
           if(cur>9) {
               carry = 1;
               cur = cur-10;
           }else {
               carry = 0;
           }
           builder.insert(0,cur);
        }
        if(ifDiff) {
            while (j>=0) {
                int d = longStr.charAt(j) - '0';
                int cur = d+carry;
                if(cur>9) {
                    carry =1;
                    cur = cur-10;
                }else {
                    carry = 0;
                }
                builder.insert(0,cur);
               j--;
            }

        }
        //如果最后 j=0时，循环完成了，此时cur>9 carry=1 还需要向前进一位
        // carry = 0 不需要进一
        if(carry == 1) {
            builder.insert(0,carry);
        }
        return builder.toString();

    }

    public String solve1(String s,String t) {
        BigInteger bigInteger1 = new BigInteger(s);
        BigInteger bigInteger2 = new BigInteger(t);
        return bigInteger1.add(bigInteger2).toString();
    }



}
